<template>
  <div v-loading="loading"
       element-loading-text="拼命加载中"
       element-loading-spinner="el-icon-loading"
       element-loading-background="rgba(0, 0, 0, 0.8)">
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入A端名称" suffix-icon="el-icon-search" v-model="a"></el-input>
      <el-input style="width: 200px;margin-left: 5px" placeholder="请输入Z端名称" suffix-icon="el-icon-search" v-model="z"></el-input>
      <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->
      <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>-->
      <el-button class="ml-5" type="primary" @click="query">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
      <el-button @click="openCompareDialogHandler" style="margin-left: 5px" type="danger">全表对比 <i
          class="el-icon-s-operation"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="a" label="A端"></el-table-column>
      <el-table-column prop="z" label="Z端"></el-table-column>

      <el-table-column prop="addressCode" label="地点编码"></el-table-column>

      <el-table-column label="操作" width="220" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">查看相似率 <i class="el-icon-search"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="(form.a && form.z)? form.a + '---' + form.z : '信息'" :visible.sync="dialogFormVisible" width="70%"
               :close-on-click-modal="false">
      <el-form label-width="120px" size="small" style="width: 50%; margin: 0 auto">
        <el-form-item label="A端">
          <el-input v-model="form.a" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="Z端">
          <el-input v-model="form.z" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地点编码">
          <el-input v-model="form.addressCode" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div v-if="sortCountA" style="display: flex;grid-gap: 5px;margin-bottom: 10px;justify-content: center;align-items: center">
        <div style="display: flex;flex-direction: column">
          <div style="margin-bottom: 10px;display: flex;grid-gap: 5px">
            <div>A端===</div>
            <el-tag size="big" type="danger">大于90%数量：{{sortCountA['>90%']}}</el-tag>
            <el-tag size="big" type="warning">大于80%数量：{{sortCountA['>80%']}}</el-tag>
            <el-tag size="big" type="primary">大于70%数量：{{sortCountA['>70%']}}</el-tag>
            <el-tag size="big" type="success">大于60%数量：{{sortCountA['>60%']}}</el-tag>
            <el-tag size="big" type="info">大于50%数量：{{sortCountA['>50%']}}</el-tag>
          </div>
          <div style="display: flex;grid-gap: 5px">
            <div>Z端===</div>
            <el-tag size="big" type="danger">大于90%数量：{{sortCountZ['>90%']}}</el-tag>
            <el-tag size="big" type="warning">大于80%数量：{{sortCountZ['>80%']}}</el-tag>
            <el-tag size="big" type="primary">大于70%数量：{{sortCountZ['>70%']}}</el-tag>
            <el-tag size="big" type="success">大于60%数量：{{sortCountZ['>60%']}}</el-tag>
            <el-tag size="big" type="info">大于50%数量：{{sortCountZ['>50%']}}</el-tag>
          </div>
        </div>
        <el-tag size="big" type="info">总数：{{ checkResult.length }}</el-tag>
      </div>
      <div style="width: 100%;display: flex;justify-content: center;">
        <el-table v-if="checkResult" :data="checkResult" border stripe :header-cell-class-name="'headerBg'"
                  :default-sort="{prop: 'aLikePercent', order: 'descending'}"
                  @selection-change="handleSelectionChange">
          <el-table-column prop="compareA" label="对比A端" align="center"></el-table-column>
          <el-table-column prop="aLikePercent" label="A端相似率" width="110" align="center" sortable>
            <template slot-scope="scope">
              <el-tag size="small" :type="scope.row.aLikePercent > 0.9 ? 'danger'
           :scope.row.aLikePercent > 0.8 ? 'warning' : scope.row.aLikePercent > 0.7 ? 'primary'
            :scope.row.aLikePercent > 0.6 ? 'success' : 'info' ">{{ Math.floor(scope.row.aLikePercent * 100) }}%
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="compareZ" label="对比Z端" align="center"></el-table-column>
          <el-table-column prop="zLikePercent" label="Z端相似率" width="110" align="center" sortable>
            <template slot-scope="scope">
              <el-tag size="small" :type="scope.row.zLikePercent > 0.9 ? 'danger'
           :scope.row.zLikePercent > 0.8 ? 'warning' : scope.row.zLikePercent > 0.7 ? 'primary'
            :scope.row.zLikePercent > 0.6 ? 'success' : 'info' ">{{ Math.floor(scope.row.zLikePercent * 100) }}%
              </el-tag>
            </template>
          </el-table-column>
        </el-table>


      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button @click="check" type="warning">计算相似度</el-button>
        <el-button type="primary" @click="save" :disabled="!hasChecked">确 定</el-button>
      </div>
    </el-dialog>


    <el-dialog title="全表对比" :visible.sync="isShowCompareDialog" width="30%"
               :close-on-click-modal="false">
      <div style="display: flex;flex-direction: column;align-items: center;font-size: 26px">
        <div>目前地址总数为：{{ azCount }}</div>
        <div>全表对比操作会耗时较长</div>
        <div>执行后请耐心等待</div>
        <div>全表对比结果会保存在数据库中</div>
        <div>执行进度请在<span style="color: red">执行进度</span>中查看</div>
      </div>


      <div slot="footer" class="dialog-footer">
        <el-button @click="isShowCompareDialog = false">取 消</el-button>
        <el-button type="primary" @click="compare">执 行</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
export default {
  name: "Az",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      uploadHeaders: {"token": localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")).token : ''},
      hasChecked: true,
      loading: false,
      checkResult: [],
      sortCountA: {},
      sortCountZ: {},
      isShowCompareDialog: false,
      azCount: '',
      a:'',
      z:''
    }
  },
  created() {
    this.load()
  },
  methods: {
    openCompareDialogHandler() {
      this.request.get("/az/getAzCount").then(res => {
        this.azCount = res.data
        this.isShowCompareDialog = true
      })
    },
    compare() {
      this.request.get("/az/compare").then(res => {
        if (res.code === '0') {
          this.isShowCompareDialog = false
          this.$message.success("执行成功")
          this.$router.push("/compareTask")
        }
      })
    },
    check() {
      if (!this.form.a || !this.form.z) {
        this.$message.error("A端和Z端不能为空")
        return
      }
      this.loading = true
      this.request.post("/az/check", this.form).then(res => {
        this.checkResult = res.data
        this.sortCountA = {'>50%': 0, '>60%': 0, '>70%': 0, '>80%': 0, '>90%': 0}
        this.sortCountZ = {'>50%': 0, '>60%': 0, '>70%': 0, '>80%': 0, '>90%': 0}
        for (let i = 0; i < this.checkResult.length; i++) {
          if (this.checkResult[i].aLikePercent > 0.9) {
            this.sortCountA['>90%']++

          } else if (this.checkResult[i].aLikePercent >= 0.8) {
            this.sortCountA['>80%']++
          } else if (this.checkResult[i].aLikePercent >= 0.7) {
            this.sortCountA['>70%']++
          } else if (this.checkResult[i].aLikePercent >= 0.6) {
            this.sortCountA['>60%']++
          } else if (this.checkResult[i].aLikePercent >= 0.5) {
            this.sortCountA['>50%']++
          }
          if (this.checkResult[i].zLikePercent > 0.9) {
            this.sortCountZ['>90%']++
          } else if (this.checkResult[i].zLikePercent >= 0.8) {
            this.sortCountZ['>80%']++
          } else if (this.checkResult[i].zLikePercent >= 0.7) {
            this.sortCountZ['>70%']++
          } else if (this.checkResult[i].zLikePercent >= 0.6) {
            this.sortCountZ['>60%']++
          } else if (this.checkResult[i].zLikePercent >= 0.5) {
            this.sortCountZ['>50%']++
          }
        }
        this.hasChecked = true
        this.loading = false
      })
    },
    load() {
      this.request.get("/az/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          a: this.a,
          z: this.z
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    reset() {
      this.pageNum = 1
      this.pageSize = 10
      this.a = ""
      this.z = ""
      this.load()
    },
    save() {
      this.request.post("/az", this.form).then(res => {
        if (res.code === '0') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.hasChecked = false
      this.dialogFormVisible = true
      this.checkResult = []
      this.sortCountA = {}
      this.sortCountZ = {}
      this.form = {}
    },
    handleEdit(row) {
      this.hasChecked = false
      this.checkResult = []
      this.sortCountA = {}
      this.sortCountZ = {}
      this.form = JSON.parse(JSON.stringify(row))
      this.loading = true
      this.request.post("/az/check", this.form).then(res => {
        this.checkResult = res.data
        this.sortCountA = {'>50%': 0, '>60%': 0, '>70%': 0, '>80%': 0, '>90%': 0}
        this.sortCountZ = {'>50%': 0, '>60%': 0, '>70%': 0, '>80%': 0, '>90%': 0}
        for (let i = 0; i < this.checkResult.length; i++) {
          if (this.checkResult[i].aLikePercent > 0.9) {
            this.sortCountA['>90%']++

          } else if (this.checkResult[i].aLikePercent >= 0.8) {
            this.sortCountA['>80%']++
          } else if (this.checkResult[i].aLikePercent >= 0.7) {
            this.sortCountA['>70%']++
          } else if (this.checkResult[i].aLikePercent >= 0.6) {
            this.sortCountA['>60%']++
          } else if (this.checkResult[i].aLikePercent >= 0.5) {
            this.sortCountA['>50%']++
          }
          if (this.checkResult[i].zLikePercent > 0.9) {
            this.sortCountZ['>90%']++
          } else if (this.checkResult[i].zLikePercent >= 0.8) {
            this.sortCountZ['>80%']++
          } else if (this.checkResult[i].zLikePercent >= 0.7) {
            this.sortCountZ['>70%']++
          } else if (this.checkResult[i].zLikePercent >= 0.6) {
            this.sortCountZ['>60%']++
          } else if (this.checkResult[i].zLikePercent >= 0.5) {
            this.sortCountZ['>50%']++
          }
        }

        this.hasChecked = true
        this.loading = false
        this.dialogFormVisible = true

      })


    },
    handleFileUploadSuccess(res) {
      this.form.file = res
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
    del(id) {
      this.request.delete("/az/" + id).then(res => {
        if (res.code === '0') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择需要删除的数据")
        return
      }
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/az/del/batch", ids).then(res => {
        if (res.code === '0') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    download(url) {
      window.open(url)
    },
    query() {
      this.pageNum = 1
      this.load()
    },
  }
}
</script>


<style>
.headerBg {
  background: #eee !important;
}
</style>
